<?php
defined('IN_TS') or die('Access Denied.');

//用户是否登录
$userid = aac('user')->isLogin();

switch ($ts) {

    //上传小组头像

    case "photo":

        $groupid = intval($_POST['groupid']);

        if($new['group']->isGroupCreater($groupid,$userid)==false&&$TS_USER['isadmin']==0){
            tsNotice("非法操作！");
        }

        //上传
        $arrUpload = tsUpload($_FILES['picfile'],$groupid,'group',array('jpg','gif','png'));

        if($arrUpload){

            $new['group']->update('group',array(
                'groupid'=>$groupid,
            ),array(
                'path'=>$arrUpload['path'],
                'photo'=>$arrUpload['url'],
            ));

            tsDimg($arrUpload['url'],'group','48','48',$arrUpload['path']);
            tsDimg($arrUpload['url'],'group','16','16',$arrUpload['path']);
            tsDimg($arrUpload['url'],'group','32','32',$arrUpload['path']);
            tsDimg($arrUpload['url'],'group','24','24',$arrUpload['path']);
            tsDimg($arrUpload['url'],'group','120','120',$arrUpload['path']);

            tsNotice("小组图标修改成功！");

        }else{
            tsNotice("上传出问题啦！");
        }

        break;

    //编辑小组基本信息
    case "edit_base":


        $groupid = intval($_POST['groupid']);

        $strGroup = $new['group']->find('group',array(
            'groupid'=>$groupid,
        ));

        if($strGroup['userid']==$userid || $TS_USER['isadmin']==1){
            $groupname = trim($_POST['groupname']);
            $groupdesc = trim($_POST['groupdesc']);
            $alias = trim($_POST['alias']);
            $notice   = trim($_POST['notice']);
            $link = trim($_POST['link']);
            $record = trim($_POST['record']);
            $donateurl = trim($_POST['donateurl']);
            $provice = trim($_POST['province']);
            $city = trim($_POST['city']);
            $address = trim($_POST['address']);
            $coordinate = trim($_POST['coordinate']);
            $contact = trim($_POST['contact']);
            $phone = trim($_POST['phone']);
			$role_leader = trim($_POST['role_leader']);
            $role_admin = trim($_POST['role_admin']);
            $role_user = trim($_POST['role_user']);
			$max = trim($_POST['max']);
			$lunch = intval($_POST['lunch']);
			$arrange = trim($_POST['arrange']);
            $openday = $_POST['openday'];
            $birth = strtotime($_POST['birth']);
            $begintime = strtotime($_POST['begintime']);
            $endtime = strtotime($_POST['endtime']);
            $coordinate = trim($_POST['coordinate']);

            if($groupname=='' || $groupdesc=='') tsNotice("小组名称和介绍都不能为空！");

            //过滤内容开始
            if($TS_USER['isadmin']!=1){
                aac('system')->antiWord($groupname);
                aac('system')->antiWord($groupdesc);
            }

            //过滤内容结束

            if($_POST['isplace']!=0&&$_POST['joinway']==2){
                tsNotice('志愿者团队小组不能设置为加入须审核');
            }

            if($openday){
                $openday = implode(',',$openday);
            }

            $isgroupname = $new['group']->findCount('group',array(
                'groupname'=>$groupname,
            ));

            if($isgroupname > 0 && $strGroup['groupname']!=$groupname) tsNotice('小组名称已经存在！');


            $new['group']->update('group',array(
                'groupid'=>$groupid,
            ),array(
                'groupname'	=> $groupname,
                'groupdesc'	=> $groupdesc,
                'alias'    =>  $alias,
                'link'      => $link,
                'notice'   =>  $notice,
                'record'   => 	$record,
                'donateurl'  => $donateurl,
                'province' =>  $provice,
                'city'     =>  $city,
                'address'  =>  $address,
                'coordinate' => $coordinate,
                'contact'  =>  $contact,
                'phone'   =>  $phone,
				'max' => $max,
				'role_leader' => $role_leader,
                'role_admin' => $role_admin,
                'role_user' => $role_user,
				'lunch' => $lunch,
				'arrange' => $arrange,
                'openday' => $openday,
                'birth'   => $birth,
                'begintime' => $begintime,
                'ispost'	=> intval($_POST['ispost']),
                'isopen'	=> intval($_POST['isopen']),
                'joinway'   =>  intval($_POST['joinway']),
                'status'	=> intval($_POST['status']),
                'isdonate'  => intval($_POST['isdonate']),
                'iscouncil'	=> intval($_POST['iscouncil']),
                'isserver'  => intval($_POST['isserver']),
                'isassist'		=> intval($_POST['isassist']),
                'ispostaudit'		=> intval($_POST['ispostaudit']),
                'isplace'		=> intval($_POST['isplace']),
            ));

            // 处理标签
            if ($_POST ['tag']) {
                aac ( 'tag' )->delIndextag ( 'group', 'groupid', $groupid );
                aac ( 'tag' )->addTag ( 'group', 'groupid', $groupid, $_POST ['tag'] );
            }

            header('Location:'.tsUrl('group','show',array('id'=>$groupid)));

        }else{

            tsNotice('非法操作！');

        }

        break;

    //删除话题
    case "deltopic":

        //普通用户不允许删除内容
        if($TS_SITE['isallowdelete'] && $TS_USER ['isadmin'] == 0) tsNotice('系统不允许用户删除内容，请联系管理员删除！');

        $topicid = intval($_GET['topicid']);

        $strTopic = $new['group']->find('group_topic',array('topicid'=>$topicid));

        $groupid = $strTopic['groupid'];

        $strGroup = $new['group']->find('group',array('groupid'=>$groupid));

        $strGroupUser = $new['group']->find('group_user',array(
            'userid'=>$userid,
            'groupid'=>$groupid,
        ));

        if ($strTopic ['addtime'] < strtotime('-7 day') && $TS_USER ['isadmin'] == 0) {
            tsNotice('超过一周，不允许删除，如有需要请给微澜图书馆公众号后台留言');
        }


        //系统管理员删除
        if($TS_USER['isadmin'] == '1'){
            $new['group']->delTopic($topicid);

            header('Location: '.tsUrl('group'));

        }

        //其他人员删除
        if($userid == $strTopic['userid'] || $userid == $strGroup['userid'] || $strGroupUser['isadmin']=='1'){

            $new['group']->update('group_topic',array(
                'topicid'=>$topicid,
            ),array(
                'isdelete'=>1,
            ));


            //处理积分
            aac('user')->doScore($GLOBALS['TS_URL']['app'],$GLOBALS['TS_URL']['ac'],$GLOBALS['TS_URL']['ts'],$strTopic['userid']);


            tsNotice('你的删除话题申请已经提交！');

        }

        break;

    //收藏话题
    case "topic_collect":

        $topicid = intval($_POST['topicid']);

        $strTopic = $db->once_fetch_assoc("select * from ".dbprefix."group_topic where topicid='".$topicid."'");

        $collectNum = $db->once_num_rows("select * from ".dbprefix."group_topic_collect where userid='$userid' and topicid='$topicid'");

        if($userid == '0'){
            echo 0;
        }elseif($userid == $strTopic['userid']){
            echo 1;
        }elseif($collectNum > 0){
            echo 2;
        }else{

            $new['group']->create('group_topic_collect',array(
                'userid'=>$userid,
                'topicid'=>$topicid,
                'addtime'=>time(),
            ));

            $new['group']->update('group_topic',array(
                'topicid'=>$topicid,
            ),array(
                'count_love'=>$strTopic['count_love']+1,
            ));

            echo 3;
        }

        break;

    //置顶话题
    case "article_istop":

        $articleid = intval($_GET['articleid']);

        $strArticle = aac('article')->find('article',array(
            'articleid'=>$articleid,
        ));

        $istop = $strArticle['istop'];

        $istop == 0 ? $istop = 1 : $istop = 0;

        $strGroup = $new['group']->find('group',array(
            'groupid'=>$strArticle['groupid'],
        ));

        if($userid==$strGroup['userid'] || $TS_USER['isadmin']==1){
            aac('article')->update('article',array(
                'articleid'=>$articleid,
            ),array(
                'istop'=>$istop,
            ));


            tsNotice("话题置顶成功！",'点击返回',tsUrl('group','show',array('id'=>$strGroup['groupid'])).'#info','true');


        }else{
            tsNotice("非法操作！");
        }
        break;

    //话题标签
    case "topic_tag_ajax";

        $topicid = intval($_GET['topicid']);
        include template("topic_tag_ajax");
        break;

    //添加话题标签
    case "topic_tag_do":

        $topicid = intval($_POST['topicid']);

        if($topicid == 0) tsNotice("非法操作！");

        $tagname = t($_POST['tagname']);
        $uptime	= time();

        if($tagname != ''){

            if(strlen($tagname) > '32') tsNotice("TAG长度大于32个字节（不能超过16个汉字）");

            $tagcount = $db->once_num_rows("select * from ".dbprefix."tag where tagname='".$tagname."'");

            if($tagcount == '0'){
                $db->query("INSERT INTO ".dbprefix."tag (`tagname`,`uptime`) VALUES ('".$tagname."','".$uptime."')");
                $tagid = $db->insert_id();

                $tagIndexCount = $db->once_num_rows("select * from ".dbprefix."tag_topic_index where topicid='".$topicid."' and tagid='".$tagid."'");
                if($tagIndexCount == '0'){
                    $db->query("INSERT INTO ".dbprefix."tag_topic_index (`topicid`,`tagid`) VALUES ('".$topicid."','".$tagid."')");
                }

                $tagIdCount = $db->once_num_rows("select * from ".dbprefix."tag_topic_index where tagid='".$tagid."'");

                $db->query("update ".dbprefix."tag set `count_topic`='".$tagIdCount."',`uptime`='".$uptime."' where tagid='".$tagid."'");

            }else{

                $tagData = $db->once_fetch_assoc("select * from ".dbprefix."tag where tagname='".$tagname."'");

                $tagIndexCount = $db->once_num_rows("select * from ".dbprefix."tag_topic_index where topicid='".$topicid."' and tagid='".$tagData['tagid']."'");
                if($tagIndexCount == '0'){
                    $db->query("INSERT INTO ".dbprefix."tag_topic_index (`topicid`,`tagid`) VALUES ('".$topicid."','".$tagData['tagid']."')");
                }

                $tagIdCount = $db->once_num_rows("select * from ".dbprefix."tag_topic_index where tagid='".$tagData['tagid']."'");

                $db->query("update ".dbprefix."tag set `count_topic`='".$tagIdCount."',`uptime`='".$uptime."' where tagid='".$tagData['tagid']."'");

            }

            echo "<script language=JavaScript>parent.window.location.reload();</script>";

        }

        break;



    //回复评论
    case "recomment":



        $referid = intval($_POST['referid']);
        $topicid = intval($_POST['topicid']);
        $content = tsClean($_POST['content']);

        $new['group']->create('group_topic_comment',array(
            'referid'=>$referid,
            'topicid'=>$topicid,
            'userid'=>$userid,
            'content'=>$content,
            'addtime'=>time(),
        ));

        header ( "Location: " . tsUrl ( 'group', 'topic', array (
                'id' => $topicid
            ) ) );
        //统计评论数
        $count_comment = $new['group']->findCount('group_topic_comment',array(
            'topicid'=>$topicid,
        ));

        //更新话题最后回应时间和评论数
        $new['group']->update('group_topic',array(
            'topicid'=>$topicid,
        ),array(
            'count_comment'=>$count_comment,
            'uptime'=>time(),
        ));

        $strTopic = $new['group']->find('group_topic',array(
            'topicid'=>$topicid,
        ));

        $strComment = $new['group']->find('group_topic_comment',array(
            'commentid'=>$referid,
        ));

        if($topicid && $strTopic['userid'] != $TS_USER['userid']){
            $msg_userid = '0';
            $msg_touserid = $strTopic['userid'];
            $msg_content = '你的话题：《'.$strTopic['title'].'》新增一条评论，快去看看给个回复吧^_^ ';
            $msg_tourl = tsUrl('group','topic',array('id'=>$topicid));
            aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
        }

        if($referid && $strComment['userid'] != $TS_USER['userid'] &&  $strComment['userid'] != $strTopic['userid']){
            $msg_userid = '0';
            $msg_touserid = $strComment['userid'];
            $msg_content = '有人评论了你在话题：《'.$strTopic['title'].'》中的回复，快去看看给个回复吧^_^ ';
            $msg_tourl = tsUrl('group','topic',array('id'=>$topicid));
            aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
        }


        break;



    case 'parseurl':
        function formPost($url,$post_data){
            $o='';
            foreach ($post_data as $k=>$v){
                $o.= "$k=".urlencode($v)."&";
            }
            $post_data=substr($o,0,-1);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            $result = curl_exec($ch);
            return $result;
        }

        $url = $_POST['parseurl'];
        $urlArr = parse_url($url);
        $domainArr = explode('.',$urlArr['host']);
        $data['type'] = $domainArr[count($domainArr)-2];
        $str = formPost('http://share.pengyou.com/index.php?mod=usershare&act=geturlinfo',array('url'=>$url));
        echo $str;

        break;

    //置顶话题
    case "isposts":

        $topicid = intval($_GET['topicid']);

        if($userid == 0 || $topicid == 0) tsNotice("非法操作");

        $strTopic = $db->once_fetch_assoc("select userid,groupid,title,isposts from ".dbprefix."group_topic where topicid='$topicid'");

        $strGroup = $db->once_fetch_assoc("select userid from ".dbprefix."group where groupid='".$strTopic['groupid']."'");

        if($userid == $strGroup['userid'] || intval($TS_USER['isadmin']) == 1){
            if($strTopic['isposts']==0){
                $db->query("update ".dbprefix."group_topic set `isposts`='1' where `topicid`='$topicid'");

                //msg start
                $msg_userid = '0';
                $msg_touserid = $strTopic['userid'];
                $msg_content = '恭喜，你的话题：《'.$strTopic['title'].'》被评为精华帖啦^_^ ';
                $msg_tourl = tsUrl('group','topic',array('id'=>$topicid));
                aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
                //msg end

            }else{
                $db->query("update ".dbprefix."group_topic set `isposts`='0' where `topicid`='$topicid'");
            }

            tsNotice("操作成功！");
        }else{
            tsNotice("非法操作！");
        }

        break;

    //小组邀请用户
    case "invite":

        $userid = intval($_POST['userid']);
        $groupid = intval($_POST['groupid']);

        if(aac('user')->isUser($userid)){

            $groupUserNum = $new['group']->find('group_user',array(
                'userid'=>$userid,
                'groupid'=>$groupid,
            ));

            if($groupUserNum ) tsNotice('用户已经加入小组！');

            $new['group']->create('group_user',array(
                'userid'=>$userid,
                'groupid'=>$groupid,
                'isvo'=>1,
                'wechat'=>1,
                'addtime'=>time(),
            ));

            //计算小组会员数
            $count_user = $new['group']->findCount('group_user',array(
                'groupid'=>$groupid,
            ));

            //更新小组成员统计
            $new['group']->update('group',array(
                'groupid'=>$groupid,
            ),array(
                'count_user'=>$count_user,
            ));

            //更新用户加群信息
            //计算小组会员数
            $count_group = $new['group']->findCount('group_user',array(
                'userid'=>$userid,
            ));

            //更新小组成员统计
            aac('userid')->update('user_info',array(
                'userid'=>$userid,
            ),array(
                'count_group'=>$count_group,
            ));

            echo 1;

        }else{
            tsNotice('倒霉了吧？');
        }

        break;

    //usertips
    case "usertips":

        $data = fileRead('data/user_tips.php');

        if($data == ''){
            $query = $db->fetch_all_assoc("select * from " . dbprefix . "user_info");
            foreach($query as $user) {
                $usertip[]=array('user'=>$user['username'],'name'=>$user['userid']);
            }
            fileWrite('user_tips.php','data',json_encode($usertip));
            $data = fileRead('data/user_tips.php');
        }

        echo $data;

        break;
    case "addacount":
        $js = intval($_GET['js']);

        $userid = intval($TS_USER['userid']);

        if ($userid == 0) {
            getJson('请登录后再登记', $js);
        }
        if(aac('user')->isPublisher()==false) tsNotice('不好意思，你还没有权限记账！');

        $groupid = trim($_POST['groupid']);
        $addtime = strtotime(trim($_POST['addTime']));
        $type = trim($_POST['type']);
        $num = trim($_POST['num']);
        $memo = trim($_POST['memo']);

        $arrGroup = aac('group')->getOneGroup($groupid);

        if ($addtime=='') {
            getJson('日期必须填写完整', $js);
        }

        if($type==''){
            getJson('请选择属性', $js);
        }

        if($num == ''){
            getJson('金额不能为空', $js);
        }
        if($memo == ''){
            getJson('明细不能为空', $js);
        }
        if ($groupid == '') {
            getJson('分馆不能为空', $js);
        }


        $arr=array('groupid' => $groupid,
            'userid' => $userid,
            'addtime' => $addtime,
            'type' => $type,
            'num' => $num,
            'memo' => $memo,);


        $acountid = $new['group']->create('group_acount', $arr);

        if($type==0){
            $outcome=$arrGroup['outcome']*1+$num*1;
            $arr1=array('outcome'=>$outcome);
        }else{
            $income=$arrGroup['income']*1+$num*1;
            $arr1=array('income'=>$income);
        }

        $new['group']->update('group',array(
            'groupid'=>$groupid,
        ),$arr1);

        /**
         * 上传附件
         */
        $arrUpload = tsUpload($_FILES['attachment'],$acountid,'group',array('jpg','jpeg','gif','png','pdf','doc','xls','xlsx'));

        if($arrUpload){
            $arr2 = array('url'=> $arrUpload['url']);
            $re = $new['group']->update('group_acount',array(
                'acountid'=>$acountid,
            ),$arr2);
        }

        getJson('记账成功。', $js, 2, tsUrl('group','acount',array('id'=>$groupid))."#info");

        break;

    //删除服务记录
    case "delacount":
        $js = intval($_GET['js']);
        $userid = intval($TS_USER['userid']);
        if ($userid == 0) {
            getJson('请登录后再操作', $js);
        }
        $acountid = trim($_POST['acountid']);
        $arrAcount = $new['group']->find('group_acount',array('acountid'=>$acountid));
        if($arrAcount['userid']!=$userid){
            getJson('您没有操作权限', $js);
        }


        $new['group']->delete('group_acount',array(
            'acountid'=>$acountid,
        ));
        $arrGroup = aac('group')->getOneGroup($arrAcount['groupid']);

        if($arrAcount['type']==0){
            $outcome=$arrGroup['outcome']*1-$arrAcount['num']*1;
            $arr1=array('outcome'=>$outcome);
        }else{
            $income=$arrGroup['income']*1-$arrAcount['num']*1;
            $arr1=array('income'=>$income);
        }
        $new['group']->update('group',array(
            'groupid'=>$arrAcount['groupid'],
        ),$arr1);

        echo 1;
        break;
    case 'delgroup':
        if($TS_SITE['isallowdelete'] && $TS_USER ['isadmin'] == 0){
            tsNotice('你没有操作权限！');
        }
        $groupid = intval($_POST['groupid']);
        $strGroup = $new['group']->getOneGroup($groupid);
        if($strGroup['count_user']>1||$strGroup['count_article']>0||$strGroup['count_duty']>0){
            tsNotice('小组不符合解散条件！');
        }
        //删除小组成员
        $new['group']->delete('group_user',array(
            'groupid'=>$groupid,
        ));
        //删除小组
        $new['group']->delete('group',array(
            'groupid'=>$groupid,
        ));
        tsNotice('操作完成');
        break;
    case "upload_excel":
        $userid = intval($TS_USER['userid']);
        if($TS_USER ['isadmin'] == 0) tsNotice('非管理员不允许导入数据！');
        $arrUpload = tsUpload($_FILES['excelData'],1,'group',array('xls','xlsx'));
        $data= importExecl('uploadfile/group/'.$arrUpload['url']);
        foreach ($data as $k => $v) {

            if ($k != 0) {
                if($v['C']=='支出'){
                    $type=0;
                }else{
                    $type=1;
                }
                $arrGroup = $new['group']->find('group', array(
                    'groupname' => $v['A'],
                ));
                $groupid=$arrGroup['groupid'];
                $addtime = intval(($v['B'] - 25569) * 3600 * 24);
                $num=$v['D'];
                $arr=array('groupid' => $groupid,
                    'addtime' => $addtime,
                    'type' => $type,
                    'num' => $num,
                    'memo' => $v['E'],
                    'userid' => $v['F'],);


                $new['group']->create('group_acount', $arr);

                if($type==0){
                    $outcome=$arrGroup['outcome']*1+$num*1;
                    $arr1=array('outcome'=>$outcome);
                }else{
                    $income=$arrGroup['income']*1+$num*1;
                    $arr1=array('income'=>$income);
                }

                $new['group']->update('group',array(
                    'groupid'=>$groupid,
                ),$arr1);

            }

        }
        getJson('导入成功。', $js, 2, tsUrl('group','acount',array('id'=>$groupid)));
        break;
    case 'output':
        $js = intval($_GET['js']);
        $groupid = trim($_GET['groupid']);
        $beginyear = trim($_GET['betime']);
        $endyear = trim($_GET['endtime']);

        if(!$groupid){
            getJson('您没有操作权限', $js);
        }


        $str = 'groupid='.$groupid;
        $strGroup=aac('group')->find('group',$str);
        $filename = $strGroup['groupname'];

        if($begintime){
            if(!$endtime){
                getJson('起止时间不完整', $js);
            }
            $str =$str.' and addtime>'.strtotime($begintime).' and addtime<'.strtotime($endtime);
            $filename = $filename.$begintime.'至'.$endtime;
        }

        //Excel表格式,这里简略写了8列
        $letter = array('A','B','C','D','E');
        //表头数组
        $tableheader = array('日期','属性','金额','备注','经手人');
        //$arrDuty = $this->findAll('duty',$str,null,null,null,null);
        $arrAcount = aac('group')->findAll('group_acount',$str,'addtime desc');
        foreach ( $arrAcount as $key => $item ){
            $addtime = date('Y-m-d',$item['addtime']);
            if($item['type']==1){
                $type='收入';
            }else{
                $type='支出';
            }

            $strUser = aac('user')->find('user_info',array('userid'=>$item['userid']));
            $data[$key]=array($addtime,$type,$item['num'],$item['memo'],$strUser['username']);
        }
        $filename=$filename.'明细账.xls';
        create_xls($data,$letter,$tableheader,$filename);

        output_xls($data,$filename);
        getJson('操作成功',1,1);
        break;

    case 'regroupuser':
        $strGroup = $new['group']->findAll('group_user','isstock=-1 or isstock=-2');
        foreach ($strGroup as $item) {
            $strUser = aac('user')->find('user_comment',array('userid'=>$item['userid']));
            $new['group']->update('group_user',array('groupid'=>$item['groupid'],'userid'=>$item['userid']),array('supporter'=>$strUser['authorid'],'uptime'=>$strUser['addtime']));
        }
        getJson('导入成功');
        break;
    case 'topic2article':
        $strTopic = aac('group')->findAll('group_topic');
        foreach($strTopic as $item){
            aac('article')->create('article',array(
                'topicid'=>$item['topicid'],
                'typeid'=>$item['typeid'],
                'groupid'=>$item['groupid'],
                'userid'=>$item['userid'],
                'title'=>$item['title'],
                'content'=>$item['content'],
                'count_comment'=>$item['count_comment'],
                'count_recommend'=>$item['count_recommend'],
                'count_view'=>$item['count_view'],
                'istop'=>$item['istop'],
                'addtime'=>$item['addtime'],
                'uptime'=>$item['uptime']
            ));
        }
        getJson('导入成功');
        break;
    case 'copycomment':
        $strComment = aac('group')->findAll('group_topic_comment');
        foreach($strComment as $item){
            $strArticle = aac('article')->find('article',array('topicid'=>$item['topicid']));
            aac('article')->create('article_comment',array(
                'articleid'=>$strArticle['articleid'],
                'referid'=>$item['referid'],
                'userid'=>$item['userid'],
                'content'=>$item['content'],
                'addtime'=>$item['addtime'],
                'commentid1'=>$item['commentid'],
            ));
        }
        $arrComment = aac('group')->findAll('article_comment','commentid1>0 and referid>0');
        foreach($arrComment as $v){
            $arr = aac('article')->find('article_comment',array('commentid1'=>$v['referid']));
            aac('article')->update('article_comment',array('commentid'=>$v['commentid']),array('referid'=>$arr['commentid']));
        }

        getJson('导入成功');
        break;
    case 'rearticle':
        $strGroup = aac('group')->findAll('group','topicid>0');
        foreach($strGroup as $v){
            $strArticle = aac('article')->find('article',array('topicid'=>$v['topicid']));
            aac('article')->update('article',array('articleid'=>$strArticle['articleid']),array('isdes'=>1));
            aac('group')->update('group',array('groupid'=>$v['groupid']),array('articleid'=>$strArticle['articleid']));

        }
        break;
    case 'groupduty':
        $strGroupUser=aac('group')->findAll('group_user');
        foreach($strGroupUser as $v){
            $sql = "select sum(term) as sums from ts_duty where userid=".$v['userid']." and status=1 and groupid=".$v['groupid'];
            $arr=$db->once_fetch_assoc($sql);
            $count_duty=$arr['sums'];
            $strDuty = aac('duty')->find('duty',array('groupid'=>$v['groupid'],'userid'=>$v['userid'],'status'=>1),null,'addtime desc');
            $lasttime = $strDuty['addtime'];
            aac('group')->update('group_user',array('groupid'=>$v['groupid'],'userid'=>$v['userid']),array('count_duty'=>$count_duty,'lasttime'=>$lasttime));
        }
        break;
    case 'setmemo':
        $groupid = intval($_POST['groupid']);
        $content =  tsClean ( $_POST ['content'] );

        if($content ==  '&lt;p&gt;&lt;br&gt;&lt;/p&gt;'){
            $content = '';
        }

        if($groupid==''){
            tsNotice('你没有操作权限');
        }

        $strGroup = $new['group']->find('group',array(
            'groupid'=>$groupid,
        ));

        if($strGroup['userid']<>$TS_USER['userid'] && $TS_USER['isadmin']<>1) {
            tsNotice('你没有操作权限');
        }

        aac('group')->update('group',array('groupid'=>$groupid),array('memo'=>$content,'uptime'=>time()));

        echo 1;

        break;
    case 'updategroup':
        $strUser = aac('user')->findAll('user_info','groupids="" and isstock<>0');
        foreach($strUser as $v){
            $groupids = getGroupIds($v['userid']);
            aac('user')->update('user_info',array('userid'=>$v['userid']),array('groupids'=>$groupids));

        }
}
